package com.lanou.projecta.news;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.projecta.R;
import com.lanou.projecta.news.game.NewsGame;
import com.lanou.projecta.news.headline.NewsHeadLine;
import com.lanou.projecta.news.jokes.JokesFragment;
import com.lanou.projecta.news.military.NewsMilitary;
import com.lanou.projecta.news.science.NewsScience;

import java.util.ArrayList;

/**
 * Created by dllo on 16/1/11.
 */
public class NewsFragment extends Fragment{
    private ArrayList<Fragment> datas;
    private NewsPagerAdapter adapter;
    private ViewPager   viewPager;
    private TabLayout tabLayout;
    private Toolbar toolbar;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news,null);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager_news);
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout_news);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle("部落资讯");
        ((AppCompatActivity) this.getActivity()).setSupportActionBar(toolbar);
        datas = new ArrayList<>();
        datas.add(new NewsHeadLine());
        datas.add(new NewsMilitary());
        datas.add(new NewsComic());
        datas.add(new NewsEntertainment());
        datas.add(new NewsFashion());
        datas.add(new NewsGame());
        datas.add(new NewsHistory());
        datas.add(new NewsHot());
        datas.add(new NewsScience());
        datas.add(new NewsSports());
        datas.add(new JokesFragment());
        adapter = new NewsPagerAdapter(getActivity().getSupportFragmentManager(),datas,getActivity());
        viewPager.setAdapter(adapter);
        tabLayout.setTabTextColors(Color.BLACK, Color.GREEN);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(adapter.getTabView(i));

            }


        }

        return view;
    }
}
